﻿using B.S.GridManage.Api.Write.Application.Command.DutyGrid;
using B.S.GridManage.Api.Write.DTOs;
using B.S.GridManage.Api.Write.InnerApi;
using B.S.GridManage.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.GridManage.Api.Write.Controllers
{
    /// <summary>
    /// 责任网格控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DudyController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ServiceDiscovery discovery;

        public DudyController(IMediator mediator,ServiceDiscovery discovery)
        {
            this.mediator = mediator;
            this.discovery = discovery;
        }
        /// <summary>
        /// 责任网格添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>

        [HttpPost]
        public   Task<ApiResult<int>> CreateDuty(CreateDutyCommand request) { 
            return mediator.Send(request);        
        }
        /// <summary>
        /// 责任网格修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>

        [HttpPut]
        public   Task<ApiResult<int>> UpdateDuty(UpdateDutyCommand request) {
            return mediator.Send(request);
        }

/// <summary>
        /// 责任网格删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>

        [HttpPut]
        public   Task<ApiResult<int>> DelDuty(DelDutyCommand request) {
            return mediator.Send(request);
        }

        /// <summary>
        /// 通过服务名,调用内部服务
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiPaging<UnitGridDto>> GetUnitGridPages(string id) {

            var serviceUrl = await discovery.GetUrl("B.S.GridManage.Api.Read");
            return await discovery.GetUnitGridPages(serviceUrl, "/api/Duty/GetUnitGridPages?Id="+id);
        }

    }
}
